package com.ydd.controller.user;


import com.ydd.context.BaseContext;
import com.ydd.dto.CancelCollectionDTO;
import com.ydd.dto.EditArticleDTO;
import com.ydd.dto.MyArticleCollectionDTO;
import com.ydd.dto.PageQueryDTO;
import com.ydd.entity.Article;
import com.ydd.entity.YddUser;
import com.ydd.result.PageResult;
import com.ydd.result.Result;
import com.ydd.service.IArticleService;
import com.ydd.service.IYddUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hgz
 * @since 2023-05-30
 */
@RestController("user")
@RequestMapping("/api/user")
@Slf4j
@Api(tags = "用户中心相关接口")
public class ArticleController {

    @Autowired
    private IArticleService iArticleService;

    @Autowired
    private IYddUserService iYddUserService;
    /**
     * 我的文章接口1
      * @param pageQueryDTO
     * @return
     */
    @GetMapping("/myArticle")
    @ApiOperation("我的文章接口")
    public Result<PageResult> myArticle(PageQueryDTO pageQueryDTO){

        log.info("我的文章接口:{}",pageQueryDTO);

        String currentId = BaseContext.getCurrentId();

        Integer id = Integer.parseInt(currentId);

        PageResult result = iArticleService.getResult(pageQueryDTO,id);

        return Result.success(result);

    }

    /**
     * 我的收藏接口1
     * @param myArticleCollectionDTO
     * @return
     */

    @ApiOperation("我的收藏接口")
    @GetMapping("/userDataList")
    public Result myCollection(MyArticleCollectionDTO myArticleCollectionDTO){

        log.info("我的收藏接口: {}",myArticleCollectionDTO);

        String currentId = BaseContext.getCurrentId();

        int userId = Integer.parseInt(currentId);

        PageResult result = iArticleService.getResultByCondition(myArticleCollectionDTO,userId);
        Map map =new HashMap();
        if (result != null){
            map.put("list", result);
            return Result.success(map);
        }

        Map nullMap = new HashMap();
        nullMap.put("count", 0);
        nullMap.put("data", new ArrayList<>());


        map.put("list", nullMap);
        return Result.success(map);

    }

    /**
     * 修改文章接口
     * @param editArticleDTO
     */

    @PostMapping("/editArticle")
    @ApiOperation("修改文章接口")
    public Result editArticle(@RequestBody EditArticleDTO editArticleDTO){

        log.info("修改文章接口：{}",editArticleDTO);

        //获取当前用户下所有文章

        Article article = new Article();

        BeanUtils.copyProperties(editArticleDTO,article);

        iArticleService.updateById(article);

        return Result.success();

    }

    /**
     * 修改用户资料1
     * @param yddUser
     */
    @PostMapping("/update")
    @ApiOperation("修改用户资料接口")
    public Result editUserInfo(@RequestBody YddUser yddUser){

        log.info("修改用户资料接口:{}",yddUser);

        String currentId = BaseContext.getCurrentId();

        Integer userId = Integer.parseInt(currentId);

        iYddUserService.updateUserInfo(yddUser,userId);

        return Result.success();
    }

    /**
     * 点赞收藏、取消点赞收藏接口
     * @param cancelCollectionDTO
     * @return
     */
    @GetMapping("/userDataHandle")
    @ApiOperation("点赞收藏、取消点赞收藏接口")
    public Result userDataHandle(CancelCollectionDTO cancelCollectionDTO){

        log.info("点赞收藏、取消点赞收藏接口cancelCollectionDTO:{}",cancelCollectionDTO);

        String userId = BaseContext.getCurrentId();

        Integer id = Integer.parseInt(userId);

        iArticleService.cancelCollection(cancelCollectionDTO,id);

        return Result.success();
    }



    /**
     * 发布文章接口
     * @param article
     */
    @ApiOperation("发布文章接口")
    @PostMapping("/addArticle")

    public Result addArticle(@RequestBody Article article){

        log.info("发布文章接口:{}",article);

        String userId = BaseContext.getCurrentId();

        Integer id = Integer.parseInt(userId);

        article.setUserId(id);

        iArticleService.save(article);

        return Result.success();
    }

    /**
     * 删除文章接口
     * @param id
     */
    @GetMapping("/delArticle")
    @ApiOperation("删除文章接口")
    public Result delArticle(Integer id){

        log.info("删除文章接口:{}",id);

        iArticleService.removeById(id);

        return Result.success();
    }
}
